# Maintainer: Tim S <stahta01@gmail.com>

_realname=wxWidgets
_wx_basever=3.1
# Example _wx_buildver value is "-rc"
_wx_buildver=

pkgbase=mingw-w64-${_realname}${_wx_basever}
pkgname=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs")
pkgver=${_wx_basever}.7
pkgrel=6
pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://wxwidgets.org/"
license=('custom:wxWindows')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "patch"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libtiff"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
options=('strip' 'staticlibs' 'buildflags' '!debug')
#options=('!strip' 'staticlibs' 'buildflags' '!debug') # Best options for debugging CodeLite crashes
source=(
  https://github.com/wxWidgets/wxWidgets/releases/download/v${pkgver}${_wx_buildver}/${_realname}-${pkgver}${_wx_buildver}.tar.bz2

  # This patch is a MSys2 run-time patch
  001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # the wxTeam rejected this patch
  005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch

  # This is needed to build clang samples in check step
  010-wxWidgets-3.1.6-Redo-manifest-filename-logic.patch

  # git master change that adds wx/filedlgcustomize.h to installation
  https://github.com/wxWidgets/wxWidgets/commit/96cb1b5d7dd208372bf93d61d5072e43b8233c29.diff
)
sha256sums=('3d666e47d86192f085c84089b850c90db7a73a5d26b684b617298d89dce84f19'
            '2b3b183a6a76ad539abc49a41033aa923c13b395c0e55189ba962068781c7135'
            '4a4828f0c9cdc638cffde6a30b5dfb14283719acc9e89e19de8ec2d5a80a5aec'
            'b30100e03f350e14060923781c3c7979735cc81996a13012bef980a23eb3d20b'
            '4750705c01eeb2b63e85c3a8e9670ebfc18e17e049067de572400181000cde18')
prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}${_wx_buildver}

  # Fix MSys2 Run-Time wx-config bug.
  patch -p1 -i "${srcdir}"/001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # This patch is not really needed; but, WX_LIBS_STATIC does not work correctly under MSys2
  # Removed it to see if anything breaks or if anything is fixed.
  patch -p1 -i "${srcdir}"/005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch

  patch -p1 -i "${srcdir}"/010-wxWidgets-3.1.6-Redo-manifest-filename-logic.patch

  patch -p1 -i "${srcdir}"/96cb1b5d7dd208372bf93d61d5072e43b8233c29.diff
}

build() {
  ####
  # Configure options added to support other software:
  #   --enable-graphics_ctx     codelite
  #   --enable-graphics-d2d     codeblocks
  #
  # Configure options added to check for build issues
  #   --disable-precomp-headers
  #
  # Configure options added to avoid possible future issues
  #   --with-cxx=14
  #   --enable-std_string
  #
  # Configure options added to avoid warnings:
  #   --with-regex=builtin
  #
  # Configure options known to cause build errors:
  #   --disable-regkey                                compile error
  #
  ####

  if check_option "buildflags" "y"; then
  # Remove the -O and -ggdb options to avoid configuration warnings
  # from the normal settings found in /etc/makepkg_mingw??.conf

    CXXFLAGS=${CXXFLAGS/-Og }
    CXXFLAGS=${CXXFLAGS/-O2 }
    CXXFLAGS=${CXXFLAGS/-ggdb }

    CFLAGS=${CFLAGS/-Og }
    CFLAGS=${CFLAGS/-O2 }
    CFLAGS=${CFLAGS/-ggdb }
  fi

  local -a _debug_options=()

  if check_option "debug" "y"; then
    _debug_options+=("--enable-debug=yes")
    _debug_options+=("--enable-debug_gdb")
  else
    _debug_options+=("--enable-optimise")
  fi

  #echo "_debug_options := ${_debug_options[@]}"

  [[ -d "${srcdir}"/build-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --enable-shared \
    --enable-std_string \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=14 \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-regex=builtin

  make #VERBOSE=1


  [[ -d "${srcdir}"/build-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  ../${_realname}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --disable-shared \
    --enable-std_string \
    --enable-iff \
    --disable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-graphics-d2d \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=14 \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-regex=builtin

  make #VERBOSE=1 -j1
}

check() {
#  cd "${srcdir}/build-${MSYSTEM}-shared/tests"    && make -k --jobs=1 || true

  cd "${srcdir}/build-${MSYSTEM}-shared/samples"  && make -k --jobs=1 || true
}

package_wxwidgets3.1-msw() {
  pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
  )
  conflicts=(
    "${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}"
    "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}"
    "${MINGW_PACKAGE_PREFIX}-wxconfig"
  )
  provides=(
    "${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}"
    "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}"
    "${MINGW_PACKAGE_PREFIX}-wxwidgets-common"
    "${MINGW_PACKAGE_PREFIX}-wxconfig"
  )
  replaces=(
    "${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}"
    "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}"
  )

  cd "${srcdir}"/build-${MSYSTEM}-static
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}"/build-${MSYSTEM}-shared
  make DESTDIR="${pkgdir}" install

  # Replace real full path with psuedo full path;
  # this seems to be needed sometime, but not always.
  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/bin/wx-config"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/msw-unicode-${_wx_basever}"
  sed -s "s|-L${MINGW_PREFIX_WIN}/lib|-L${MINGW_PREFIX}/lib|g" -i "${pkgdir}${MINGW_PREFIX}/lib/wx/config/msw-unicode-static-${_wx_basever}"

  # create wx-config copy with version file suffix
  cp ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-${_wx_basever}}

  # create wxwin.m4 copy with version file suffix
  cp ${pkgdir}${MINGW_PREFIX}/share/aclocal/wxwin.m4 ${pkgdir}${MINGW_PREFIX}/share/aclocal/wxwin${_wx_basever}.m4

  rm    "$pkgdir"${MINGW_PREFIX}/bin/*.dll
}

package_wxwidgets3.1-msw-libs() {
  pkgdesc="wxMSW shared libraries for wxwidgets ${_wx_basever} (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-expat"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-libtiff"
           "${MINGW_PACKAGE_PREFIX}-xz"
           "${MINGW_PACKAGE_PREFIX}-zlib")

  cd "${srcdir}"/build-${MSYSTEM}-shared
  make DESTDIR="${pkgdir}" install

  rm    "$pkgdir"${MINGW_PREFIX}/bin/wx-config
  rm    "$pkgdir"${MINGW_PREFIX}/bin/wxrc-${_wx_basever}
  rm    "$pkgdir"${MINGW_PREFIX}/bin/*.exe
  rm -r "$pkgdir"${MINGW_PREFIX}/share
  rm -r "$pkgdir"${MINGW_PREFIX}/include
  rm -r "$pkgdir"${MINGW_PREFIX}/lib

  # License files
  cd "${srcdir}"/${_realname}-${pkgver}${_wx_buildver}/docs
  install -Dm644 preamble.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/preamble.txt"
  install -Dm644 licence.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licence.txt"
  install -Dm644 licendoc.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licendoc.txt"
  install -Dm644 lgpl.txt     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/lgpl.txt"
  install -Dm644 gpl.txt      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/gpl.txt"
  install -Dm644 xserver.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/xserver.txt"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
